package org.chispa.api.util;

class Bin {
	private long id;
	private Double lower;
	private Double upper;
	enum Relation{OVERLAP,LESS};
	
	Bin(Double lower,Double upper,long id)
	{
		this.id=id;
		this.lower = lower;
		this.upper = upper;
	}
	
	public boolean overLap(Bin other,Relation rel)
	{
		if(rel.equals(Relation.OVERLAP))
			return doOverLap(other);
		return less(other);
	}
	
	private boolean doOverLap(Bin other)
	{
		Double oLower=other.lower;
		Double oUpper=other.upper;
		if(oLower==null)
		{
			return contains(oUpper);
		}
		if(oUpper==null)
		{
			return contains(oLower);
		}
		return contains(oLower) || contains(oUpper);
	}
	
	private boolean contains(Double what)
	{
		if(lower==null)
		{
			return (what<=upper);
		}
		if(upper==null)
		{
			return (what>lower);
		}
		return (what<=upper)&&(what>lower);
	}
	
	private boolean less(Bin other)
	{
		Double oUpper=other.upper;
		if(oUpper==null || lower==null)
		{
			return true;
		}
		return (oUpper>=lower);
	}

	public long getId() {
		return id;
	}
}
